<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/16
 * Time: 14:51
 */
namespace app\api\controller;
use think\Db;
class Part extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
        //首页列表
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.p_status'] = array('IN',array(1,2));
        $list = Db::table('up_part')->alias('a')
            ->join('up_part_theme upt','upt.part_theme_id = a.p_theme','left')
            ->field('a.*,upt.part_theme_name')
            ->where($where)
            ->order('a.create_time desc')
            ->limit($limit,$num)
            ->select();
        foreach ($list as $key=>&$vo){
            if($vo['p_user_id']==$user_id){//证明是我自己发起的
                $vo['my_owner'] = 1;
            }else{
                $vo['my_owner'] = 2;
            }
            $map['m_user_id'] =$user_id;
            $map['part_id'] =$vo['part_id'];
            $map['is_owner'] =1;
            $res = Db::table('up_part_member')->where($map)->find();
            if($res['member_id']==''){//证明这个聚会自己没有参加
                $vo['me_is_owner'] = 5;
            }else{
                $vo['me_is_owner'] = $res['m_status'];
            }
        }
        return  json(array('status'=>200,'msg'=>$list));
    }

    //我的聚会
    public function myPart(){
        extract(input());
        $month_3 = date("Y-m-d H:i:s",(time()-(3600*24*90)));
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.m_status'] = array('IN',array(1,2));
        $where['a.m_user_id'] = $user_id;
        $where['up.p_status'] = array('IN',array(1,2));
        $where['up.p_start_time'] = ['>=',$month_3];
        $list = Db::table('up_part_member')->alias('a')
            ->join('up_part up','up.part_id = a.part_id','left')
            ->join('up_part_theme upt','upt.part_theme_id = up.p_theme','left')
            ->where($where)
            ->field('upt.part_theme_name,a.*,up.part_name,up.p_start_time,up.p_address,up.p_person_num,up.p_status')
            ->order('up.p_status asc,up.p_start_time asc')
            ->limit($limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //聚会主题
    public function themePart(){
        $list = Db::table('up_part_theme')
                    ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //聚会发放奖励
    public function rewardPart(){
        $list = Db::table('up_reward')
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //创建聚会
    public function addPart(){
        extract(input());
        $user_gender = Db::table('up_u_user')->where('user_id',$user_id)->value('user_gender');
        if($user_gender ==2){
            return  json(array('status'=>100,'msg'=>"女性用户不能创建聚会"));
        }
        $part_name = input('param.part_name');//聚会名称
        $part_theme = input('param.part_theme');//主题
        $start_time = input('param.start_time');//聚会开始时间
        $part_address = input('param.part_address');//聚会地点
        $p_person_num = input('param.p_person_num');//聚会邀请人数
        $p_reward = input('param.p_reward');//聚会奖励
        $is_anonymous = input('param.is_anonymous');//1不匿名 2匿名
        $is_provide = input('param.is_provide');//1提供酒店  2不提供酒店
        $p_content = input('param.p_content');//聚会介绍
        //查询一个人送多少金币
        $gold_price = Db::table('up_reward')->where('reward_id',$p_reward)->value('reward_price');
        //查询这个人的账户金币数量
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        //俱乐部送出去的每个人金币的10倍大于账户金币才能创建俱乐部
        if((($gold_price*$p_person_num)+100)>$user_balance){//金币不足
            return  json(array('status'=>100,'msg'=>"金币不足不能创建聚会,请先充值"));
        }else{//账户金币充足的情况下
            //插入聚会表
            $data['part_name'] = $part_name;
            $data['p_user_id'] = $user_id;
            $data['p_theme'] = $part_theme;
            $data['p_start_time'] = $start_time;
            $data['p_address'] = $part_address;
            $data['p_person_num'] = $p_person_num;
            $data['p_reward'] = $p_reward;
            $data['is_anonymous'] = $is_anonymous;
            $data['is_provide'] = $is_provide;
            $data['p_content'] = $p_content;
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $part_id=Db::table('up_part')->insertGetId($data);
            //查询聚会成员表
            $result['m_user_id'] = $user_id;
            $result['part_id'] = $part_id;
            $result['join_time'] = date('Y-m-d H:i:s',time());
            $result['m_status'] = 2;
            $result['is_owner'] = 2;
            $res1 = Db::table('up_part_member')->insertGetId($result);
            //更新男性用户消费记录  todo
            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_consume',(($gold_price*$p_person_num)+100));
            //更新男性用户账号余额
            Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',(($gold_price*$p_person_num)+100));
            if($part_id&&$res1){
                return  json(array('status'=>200,'msg'=>"创建成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"创建失败"));
            }
        }
    }
    //聚会详情
    public function detailPart(){
        extract(input());
        $part_id = input('param.part_id');
        $where['a.part_id'] = $part_id;
        $list = Db::table('up_part')->alias('a')
            ->join('up_part_theme upt','upt.part_theme_id = a.p_theme','left')
            ->join('up_u_user uuu','uuu.user_id = a.p_user_id','left')
            ->join('up_reward ur','ur.reward_id = a.p_reward','left')
            ->field('a.p_address,a.part_id,a.part_name,a.p_user_id,a.p_start_time,
            a.p_person_num,a.is_provide,a.is_anonymous,
            upt.part_theme_name,uuu.user_username,uuu.user_image,ur.reward_price')
            ->where($where)
            ->find();
        //统计累计报名多少人
        $map['is_owner'] = 1;
        $map['part_id'] = $list['part_id'];
//        $map['m_status'] = 1;
        $list['num'] = Db::table('up_part_member')->where($map)->count();
        //通过了多少人
        $map1['is_owner'] = 1;
        $map1['part_id'] = $list['part_id'];
        $map1['m_status'] = 2;
        $list['adopt'] = Db::table('up_part_member')->where($map1)->count();
        //VIP等级
        $list['vip'] = $this->getVip($list['p_user_id']);
        return  json(array('status'=>200,'msg'=>$list));
    }

    //申请列表
    public function adoptPart(){
        extract(input());
        $part_id = input('param.part_id');
        $where['a.part_id'] = $part_id;
        $where['a.m_status'] = 1;
        $where['a.is_owner'] = 1;
        $list = Db::table('up_part_member')->alias('a')
             ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
             ->where($where)
             ->field('a.member_id,a.part_id,a.member_id,a.m_user_id,uuu.user_username,uuu.user_image,uuu.user_gender')
             ->select();
        foreach ($list as $key=>&$vo){
            if($vo['user_gender']==2){
                $vo['vip'] = 1;
            }else{
                $vo['vip'] = $this->getVip($vo['m_user_id']);
            }
        }
        //查询多少人申请
        $map['is_owner'] = 1;
        $map['part_id'] = $part_id;
        $map['m_status'] = 1;
        $result['num'] = Db::table('up_part_member')->where($map)->count();
        $result['list'] = $list;
        return  json(array('status'=>200,'msg'=>$result));
    }
    //申请通过聚会
    public function clickAdopt(){
        extract(input());
        $part_id = input('param.part_id');//聚会ID
        $member_id = input('param.member_id');//聚会申请ID
        //查询这个聚会邀请了多少人
        $p_person_num = Db::table('up_part')->where('part_id',$part_id)->value('p_person_num');
        //查询这个聚会申请通过了多少人
        $where['part_id'] = $part_id;
        $where['m_status'] = 2;
        $where['is_owner'] =1;
        $num = Db::table('up_part_member')->where($where)->count();
        if($num>=$p_person_num){//申请人数已经超过邀请人数
            return  json(array('status'=>100,'msg'=>"通过申请人数已经超过邀请人数"));
        }else{
            $data['m_status'] = 2;
            $data['join_time'] = date('Y-m-d H:i:s',time());
           $res =  Db::table('up_part_member')->where('member_id',$member_id)->update($data);
           $info =  Db::table('up_part_member')->where('member_id',$member_id)->find();
            if($res){
                //推送
                $list1 = [];
                $v = (string)$info['m_user_id'];
                array_push($list1, $v);
                $code=2;
                $str = array('msg'=>'您的聚会已经通过申请','type'=>9,'part_id'=>$part_id);
                $str1 = '您的聚会已经通过申请';
                try{
                    $this->Jpush1(1, $list1, $str1, $str,$code);
                }catch(\Exception $e){

                }
                return  json(array('status'=>200,'msg'=>"成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"失败"));
            }
        }

    }
    //申请加入聚会
    public function applyPart(){
        extract(input());
        $part_id = input('param.part_id');//聚会ID
        $is_aircraft = input('param.is_aircraft');
        $is_hotel = input('param.is_hotel');
        $arrive_time = input('param.arrive_time');
        $data['part_id'] = $part_id;
        $data['m_user_id'] = $user_id;
        $data['is_aircraft'] = $is_aircraft;
        $data['is_hotel'] = $is_hotel;
        $data['arrive_time'] = $arrive_time;
        $res = Db::table('up_part_member')->insertGetId($data);
        if($res){
            $info = Db::table('up_part')->where('part_id',$part_id)->find();
            $info1 = Db::table('up_u_user')->where('user_id',$user_id)->find();
            //推送
            $list1 = [];
            $v = (string)$info['p_user_id'];
            array_push($list1, $v);
            $code=2;
            $str = array('msg'=>$info1['user_image'].'申请您的聚会','type'=>9,'part_id'=>$part_id);
            $str1 =$info1['user_image'].'申请您的聚会';
            try{
                $this->Jpush1(1, $list1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return  json(array('status'=>200,'msg'=>"申请成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"申请失败"));
        }
    }

    //聚会发放奖励
    public function grant(){
        extract(input());
        $part_id = input('param.part_id');//聚会ID
        //查询这个聚会信息
        $list = Db::table('up_part')->where('part_id',$part_id)->find();
        if($list['is_true']==2){//没有更新发放奖励状态
            Db::table('up_part')->where('part_id',$part_id)->setField('is_true',1);
        }
        //查询这个聚会每个人发放多少奖励
        $reward = Db::table('up_reward')->where('reward_id',$list['p_reward'])->find();
        //查询申请通过的人
        $where['part_id'] = $part_id;
        $where['m_status'] = 2;
        $where['is_owner'] = 1;
        $res = Db::table('up_part_member')->where($where)->select();
        //循环发放奖励
        foreach ($res as $key=>$vo){
            Db::table('up_u_user')->where('user_id',$vo['m_user_id'])->setInc('user_balance',$reward['reward_price']);
        }
        return  json(array('status'=>200,'msg'=>""));
    }


}